Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in Java which are given below
Operator Type | Category | Precedence |
---|---|---|
Unary | postfix prefix |
expr++ expr-- ++expr --expr +expr -expr |
Arithmetic | multiplication/divison addition/subtraction |
* / % + - |
Shift | shift | <<>> >>> |
Relational | comparison equality |
<>
<=>= instanceof == != |
Bitwise | bitwise AND bitwise exclusive OR bitwise inclusive OR |
& ^ | |
Logical | logical AND logical OR |
&& || |
Ternary | ternary | ? : |
Assignment | assignment | = += -= *= /= %= &= ^= |= <<=>>= >>>= |
The Java unary operators require only one operand. Unary operators are used to perform various operations i.e.
// Java Unary Operator Example : ++ and --
import java.io.*;
public class UnaryOperator{
public static void main(String args[ ]){
int x = 20; // In postfix value is first used then increment by 1
System.out.println("X : "+ x++); // 20, after this line value is increase to 21 will result in next line
System.out.println("X : "+ x); // it will print 21
System.out.println("X : "+ ++x); // The variable's value initially rises by 1, and then it is utilised in the expression
System.out.println("X : "+ x--); // it will print 22
System.out.println("X : "+ --x); // it will print 20
}
}
Output:
X : 20
X : 21
X : 22
X : 22
X : 20
// Java Unary Operator Example : ~ and !
import java.io.*;
public class UnaryOperator{
public static void main(String args[ ]){
int x = 20;
int y = -20;
boolean a = true ;
boolean b = false ;
System.out.println(~x); // -21, minus of total positive value which starts from 0
System.out.println(~y); // 19, positive of total minus, positive starts from 0
System.out.println(!a); // false opposite of ture boolean value
System.out.println(!b); // true opposite of false boolean value
}
}
Output:
-21
19
false
true
Java arithmetic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations.
import java.io.*;
public class UnaryOperator{
public static void main(String args[ ]){
int x = 20;
int y = 10;
System.out.println(x+y); // 30
System.out.println(x-y); // 10
System.out.println(x*y); // 200
System.out.println(x/y); // 2
System.out.println(x%y); // 0
}
}
Output:
30
10
200
2
0
The Java left shift operator <<
is used to shift all of the bits in a value to the left side of a specified number of times.
import java.io.*;
public class UnaryOperator{
public static void main(String args[ ]){
System.out.println(2<<2); // 2*2^2=2*4=8
System.out.println(5<<3); // 5*2^3=5*8=40
System.out.println(3<<5); // 3*2^5=3*32=96
System.out.println(8<<3); // 8*2^2=8*4=32
System.out.println(15<<4); // 15*2^4=15*16=240
}
}
Output:
8
40
96
32
240
The Java left shift operator <<
is used to shift all of the bits in a value to the left side of a specified number of times.
import java.io.*;
public class UnaryOperator{
public static void main(String args[ ]){
System.out.println(20>>2); // 20/2^2=20/4=5
System.out.println(40>>3); // 40/2^3=40/5=8
System.out.println(20>>2); // 20/2^2=20/4=5
System.out.println(32>>4); // 32/2^4=32*16=2
System.out.println(20>>3); // 20/2^3=20/8=2
}
}
Output:
5
8
5
2
2
>> is arithmetic shift right, >>> is logical shift right.
import java.io.*;
public class UnaryOperator{
public static void main(String args[ ]){
System.out.println(Integer.toBinaryString(121)); // prints 1111001
System.out.println(Integer.toBinaryString(121 >> 1)); // prints 111100
System.out.println(Integer.toBinaryString(121 >>> 1)); // prints 111100
}
}
Output:
1111001
111100
111100
The logical && operator doesn't check the second condition if the first condition is false. It checks the second condition only if the first one is true.
The bitwise & operator always checks both conditions whether first condition is true or false.
// Java AND Operator Example: Logical && and Bitwise &
import java.io.*;
public class LogicalOperator{
public static void main(String args[ ]){
int x = 20;
int y = 10;
int z = 5;
System.out.println(x<y && y>z); // false && true =false
System.out.println(z>y & y<x); // ture && false = false
}
}
Output:
false
false
// Java AND Operator Example 2: Logical && and Bitwise &
import java.io.*;
public class LogicalOperator{
public static void main(String args[ ]){
int x = 8;
int y = 4;
int z = 15;
System.out.println(x<y && x++ <z); //false && true = false
System.out.println(x); // value of x is not increase as second condition is not checked
System.out.println(x<y & x++ <z); //false && true = false
System.out.println(x); // value of x is increase as second condition is checked
}
}
Output:
false
8
false
9
The logical || operator doesn't check the second condition if the first condition is true. It checks the second condition only if the first one is false.
The bitwise | operator always checks both conditions whether first condition is true or false.
// Java AND Operator Example: Logical || and Bitwise |
import java.io.*;
public class LogicalOperator{
public static void main(String args[ ]){
int x = 8;
int y = 4;
int z = 15;
System.out.println(x>y || x<z); //true || true = true
System.out.println(x>y | x<z); //true | true = true
System.out.println(x>y | x++ <z); //true | true = true
System.out.println(x); // value of x is increase as second condition is checked
}
}
Output:
ture
ture
true
9
Java Ternary operator is used as one line replacement for if-then-else statement and used a lot in Java programming. It is the only conditional operator which takes three operands.
// Java AND Operator Example: Logical || and Bitwise |
import java.io.*;
public class LogicalOperator{
public static void main(String args[ ]){
int x = 8;
int y = 4;
int minimum = (x<y)?x:y;
System.out.println(minimum);
}
}
Output:
4
Java assignment operator is one of the most common operators. It is used to assign the value on its right to the operand on its left.
// Java Assignment Operator Example
import java.io.*;
public class LogicalOperator{
public static void main(String args[ ]){
int x = 8;
int y = 4;
x= x+4;
y= y-4;
System.out.println(x); //8+4=12
System.out.println(y); // 4-4=0
}
}
Output:
12
0
Post your comment